package algorithms;

import ioUtility.FileAccess;

import java.io.*;

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.*;

import java.util.regex.*;

class FBWKPair{
	String entityname;
	String fbtype;
	String fbprop;
	String fbvalue;
	
	String wkattr;
	String wkinfo;
	String wkvalue;
	
	double propsimi;
	double valuesimi;

}

class MyDB {
	Connection conn;
	Statement stmt;

	public MyDB() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			conn = DriverManager.getConnection(
					"jdbc:mysql://pardosa06.cs.washington.edu/freebase",
					"clzhang", "wiki");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getValueFromType(String guid,String entityname) {
		String retval="";
		try {
			ResultSet res = stmt
					.executeQuery("select value from type where guid='"+guid+"'");
			while (res.next()) {
				String value = res.getString("value");
				if(!value.equals(entityname)){
					retval += value+", ";
				}
			}
		} catch (Exception e) {

		}
		return retval;
	}
	
	public String rawValue2String(String raw,String entityname){
		String result="";
		String []abc = raw.split(",");
		for(int i=0;i<abc.length;i++){
			if(abc[i].contains("/guid/")){
				String query = abc[i].replace("/guid/", "");
				result += getValueFromType(query,entityname);
			}
			else{
				result+=abc[i];
			}
		}
		return result;
	}
}

public class FBPropMapWikiAttr {

	public static void main(String[] args) {
		// System.out.println(Stemmer.stemStr("einstein"));
		String a = "spouse";
		String b = "spouse_s";
		System.out.println(cmpValue(a, b));
		System.out.println(cmpProp(a, b));
	}

	public static void main2(String[] args) {
		String xml = "<xyz>haha</xyz>";
		// Document d = XMLDocumentUtility.loadXML(xml);
		String a = xml.replaceAll("\\<.*?\\>", "");

		System.out.println(a);

		StringBuffer sb = FileAccess.readAccordUserInput("file:");
		// sb = Html2Text.html2text(sb.toString());

	}

	public static String getInfoboxValue(String a) {
		return a.replaceAll("\\<.*?\\>", "");
	}



	public static void cmpPair(String prop, String propValue, String attribute,
			String attrValue) {
		// if prop is the same as attributes, then ...
		//
	}

	// public static void cmpStrin
	public static double cmpValue(String value1, String value2) {
		Pattern p = Pattern.compile("\\p{Punct}|\\s");
		String[] list1 = p.split(value1);
		String[] list2 = p.split(value2);

		HashMap<String, Integer> dictionary = new HashMap<String, Integer>();
		int maxid = 0;

		for (int i = 0; i < list1.length; i++) {
			list1[i] = Stemmer.stemStr(list1[i]);
			if (!dictionary.containsKey(list1[i])) {
				dictionary.put(list1[i], maxid++);
			}
		}
		for (int i = 0; i < list2.length; i++) {
			list2[i] = Stemmer.stemStr(list2[i]);
			if (!dictionary.containsKey(list2[i])) {
				dictionary.put(list2[i], maxid++);
			}
		}
		int[] vec1 = new int[maxid];
		int[] vec2 = new int[maxid];
		for (int i = 0; i < list1.length; i++)
			vec1[dictionary.get(list1[i])]++;
		for (int i = 0; i < list2.length; i++)
			vec2[dictionary.get(list2[i])]++;

		int vec1len = 0;
		int vec2len = 0;
		int up = 0;
		for (int i = 0; i < maxid; i++) {
			vec1len += vec1[i] * vec1[i];
			vec2len += vec2[i] * vec2[i];
			up += vec1[i] * vec2[i];
		}

		return up * 1.0 / Math.sqrt(vec1len * vec2len);
		// for(int i=0;i<list1.length){
		//			
		// }
	}

	public static double cmpProp(String s1, String s2) {
		char s[] = s1.toCharArray();
		char t[] = s2.toCharArray();
		int n = s.length;
		int m = t.length;
		int[][] DTW = new int[n][m];
		n = n - 1;
		m = m - 1;
		int cost = 0;
		for (int i = 1; i <= n; i++) {
			DTW[i][0] = Integer.MAX_VALUE;
		}
		for (int j = 1; j <= m; j++) {
			DTW[0][j] = Integer.MAX_VALUE;
		}
		DTW[0][0] = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				cost = s[i] == t[j] ? 0 : 1;
				int whichmin = Math.min(Math.min(DTW[i - 1][j], DTW[i][j - 1]),
						DTW[i - 1][j - 1]);
				DTW[i][j] = cost + whichmin;
			}
		}
		int bigger = Math.max(n, m);
		return (bigger - DTW[n][m]) * 1.0 / bigger;
	}
}
